<?php

namespace app\bews\controller;

use app\bews\model\Base;
use think\app;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Bew extends Base
{
    public function menuInfo(){
        $lists = Db::table("bew_admin_sys_menu")->where("parent_id",0)->order("sort DESC,smid")->select();
        View::assign([
           'lists'=>$lists
        ]);
        return View::fetch();
    }

    public function menuAdd(){
        if(Request::isPost()){
//            print_r(input('post.'));
            $label = input('post.label');
            $sort = input('post.sort',0);
            $status = input('post.status',1);
            $insert = Db::table('bew_admin_sys_menu')->insert([
                'label'=>$label,
                'sort'=>$sort,
                'status'=>$status
            ]);
            if(empty($insert)){
                echo json_encode(['code'=>1,'msg'=>'添加失败,请重试']);
                exit();
            }else{
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
            }
        }else{
            return View::fetch();
        }

    }

    public function menuModify(){
        if(Request::isPost()){
            $smid = input('post.smid');
            $label = input('post.label');
            $sort = input('post.sort',0);
            $status = input('post.status',1);
            $update = Db::table("bew_admin_sys_menu")->where('smid',$smid)->update([
                'label'=>$label,
                'sort'=>$sort,
                'status'=>$status
            ]);
            if($update == null){
                echo json_encode(['code'=>1,'msg'=>'修改失败,请重试']);
                exit();
            }
            echo json_encode(['code'=>0,'msg'=>'修改成功']);
        }else{
            $smid = input('get.smid');
            if(empty($smid)){
                $this->error("请传入导航id");
                exit();
            }
            $find = Db::table("bew_admin_sys_menu")->where('smid',$smid)->find();
            View::assign([
                'find'=>$find
            ]);
            return View::fetch();
        }

    }

    public function menuDel(){
        $smid = input('get.smid');
        if(empty($smid)){
            $this->error("请传入导航id");
            exit();
        }
        $del = Db::table("bew_admin_sys_menu")->where('smid',$smid)->delete();
        if($del == null){
            echo json_encode(['code'=>1,'msg'=>'删除失败,请重试']);
            exit();
        }
        echo json_encode(['code'=>0,'msg'=>'删除成功']);
        return View::fetch();
    }

    public function buttonInfo(){
        $smid = input('get.smid');
        $lists = Db::table("bew_admin_sys_menu")->where('parent_id',$smid)->select()->toArray();
        View::assign([
            'lists'=>$lists,
            'smid'=>$smid
        ]);
        return View::fetch();
    }

    public function buttonAdd(){
        if(Request::isPost()){
            $smid = input('post.smid');
            if($smid == null){
                echo json_encode(['code'=>1,'msg'=>'请传入id']);
                exit();
            }
//            print_r(input('post.'));
            $label = input('post.label');
            $src = input('post.src');
            $sort = input('post.sort',0);
            $status = input('post.status',1);
            $insert = Db::table('bew_admin_sys_menu')->insert([
                'label'=>$label,
                'sort'=>$sort,
                'status'=>$status,
                'parent_id'=>$smid,
                'type'=>1,
                'src'=>$src
            ]);
            if(empty($insert)){
                echo json_encode(['code'=>1,'msg'=>'添加失败,请重试']);
                exit();
            }else{
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
            }
        }else{
            View::assign([
                'smid'=>input('get.smid')
            ]);
            return View::fetch();
        }
    }

    public function buttonModify(){
        if(Request::isPost()){
            $smid = input('post.smid');
            $label = input('post.label');
            $sort = input('post.sort',0);
            $status = input('post.status',1);
            $src = input('post.src');
            $update = Db::table("bew_admin_sys_menu")->where('smid',$smid)->update([
                'label'=>$label,
                'sort'=>$sort,
                'status'=>$status,
                'src'=>$src
            ]);
            if($update == null){
                echo json_encode(['code'=>1,'msg'=>'修改失败,请重试']);
                exit();
            }
            echo json_encode(['code'=>0,'msg'=>'修改成功']);
        }else{
            $smid = input('get.smid');
            if(empty($smid)){
                $this->error("请传入导航id");
                exit();
            }
            $find = Db::table("bew_admin_sys_menu")->where('smid',$smid)->find();
            View::assign([
                'find'=>$find
            ]);
            return View::fetch();
        }
    }

}